<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2019/5/13
 * Time: 13:27
 */
namespace Modules\ApiV2\Controllers;

use App\Models\OilCard;
use App\Models\Team;
use App\Models\User;
use App\Models\UserCoupon;
use function GuzzleHttp\Psr7\str;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Modules\Token\Facades\Token;

class ShareController extends BaseController {
    private $coupon = [23,24,25,26,27];

    //活动状态
    public function teamStatus(){
        if (strtotime(Team::START_TIME)>time()){
            return self::fail(1043,"活动未开始");
        }
        if (strtotime(Team::END_TIME)<time()){
            return self::fail(1044,"活动已结束");
        }
        return self::success();
    }


    //新用户领取优惠券
    public function NewUserGetCoupon(Request $request){
        try {
            $userId = Token::data("id");
            $user = User::find($userId);
            if (!$user) return self::fail(1014, "请登录后再操作哟!");
            //查询用户是否领取
            $userCouponData = UserCoupon::query()->where("user_id", $userId)->where("coupon_id", $this->coupon[0])->first();
            if ($userCouponData) {
                return self::fail(1039, "该优惠券只能领取一次,请不要重复领取!");
            }
            if (strtotime($user->created_at) < strtotime(Team::START_TIME)) {
                return self::fail(1038, "您已不是新用户,无法领取该优惠哟!");
            }
            //赠送优惠券
            UserCoupon::sendCouponToUser($this->coupon[0], $userId);
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::fail(500,"系统异常");
        }
    }

    //发起开团
    public function startTeam(Request $request){
        try{
            if (strtotime(Team::START_TIME)>time()){
                return self::fail(1043,"活动未开始!");
            }
            if (strtotime(Team::END_TIME)<time()){
                return self::fail(1044,"活动已结束!");
            }
            $userId = Token::data("id");
            $user = User::find($userId);
            if (!$user)return self::fail(1014,"请登录后在操作哟!");
            //判断用户是否已开过团
            $teamData = Team::where("user_id",$userId)->where("role","leader")->first();
            if (!$teamData) {
                if ($user->recharge_amount > 0) {
                    $isRecharge = 1;
                } else {
                    $isRecharge = 0;
                }
                //实例化对象
                $team = new Team();
                $team->team_code = Team::createTeamCode($user->open_code);
                $team->user_id = $userId;
                $team->mobile = $user->mobile;
                $team->role = "leader";
                $team->is_recharge = $isRecharge;
                $team->save();
            }

            $data = [
                'share_title' => "好友组团，新手折上折!",
                'share_desc'  => "邀友组团领150元优惠券，好友更享折上9.8折充值折扣！",
                'share_img'   => "https://wzwh.oss-cn-hangzhou.aliyuncs.com/OIL_CARD/20190624/a9557d00e7e043e1313ead2d20ce8ccf.png",
                'share_link'  => "http://h5.wenhuakj.com/groupBook"
            ];
            return self::success($data);
        }catch (\Exception $e){
            Log::error($e);
            return self::fail(500,"系统异常");
        }
    }

    //点击领取活动奖励
    public function getActivityAward(Request $request){
        try{
            $userId = Token::data("id");
            $user = User::find($userId);
            if (!$user)return self::fail(1014,"请登录后在操作哟!");
            //获取参数
            $num = $request->input("num",false);
            if (!$num)return self::fail(1003,"非法请求,请选择领取奖励!");
            //查询用户是否已经领取过
            $userCoupon = UserCoupon::where("coupon_id",$this->coupon[$num])->where("user_id",$userId)->count();
            if ($userCoupon)return self::fail(1039,"该优惠券只能领取一次,请不要重复领取!");
            UserCoupon::sendCouponToUser($this->coupon[$num],$userId);
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::fail(500,"系统异常");
        }
    }

    //有效好友
    public function teamMemberList(){
        try{
            $userId = Token::data("id");
            $user = User::find($userId);
            if (!$user)return self::fail(1014,"请登录后再操作!");
            //查询我的队伍
            $team = Team::where("user_id",$userId)->where("role","leader")->first();
            if (!$team)return self::fail(1042,"找不到您的团队");
            //查询我的团员
            $teamUserCount = Team::where("team_code",$team->team_code)->where("role","member")->where("status",1)->count();
            $teamUserData = Team::where("team_code",$team->team_code)->where("role","member")->orderBy("id","asc")->select("id","mobile","is_recharge","remark","created_at")->get();
            //查询我的领取记录
            $getArray = [];
            foreach ($this->coupon as $item){
                $records = UserCoupon::where("user_id",$userId)->where("coupon_id",$item)->count();
                if ($records){
                    $getArray[] = 2;
                }else{
                    $getArray[] = 0;
                }
            }
            $userArray = [];
            foreach ($teamUserData as $item){
                $registerDate = date('Y-m-d',strtotime($item['created_at']));
                $item['register_time'] = $registerDate;
                $userArray[] = $item;
            }
            $item = ["count"=>$teamUserCount,"data"=>$userArray,"new_coupon"=>$getArray[0],"coupon1"=>$getArray[1],"coupon2"=>$getArray[2],"coupon3"=>$getArray[3],"coupon4"=>$getArray[4]];
            if ($teamUserCount>=2 && $teamUserCount<5){
                if ($item['coupon1']==0) {
                    $item['coupon1'] = 1;
                }
            }elseif ($teamUserCount>=5 && $teamUserCount<8){
                if ($item['coupon1']==0) {
                    $item['coupon1'] = 1;
                }
                if ($item['coupon2']==0) {
                    $item['coupon2'] = 1;
                }
            }elseif ($teamUserCount>=8 && $teamUserCount<10){
                if ($item['coupon1']==0) {
                    $item['coupon1'] = 1;
                }
                if ($item['coupon2']==0) {
                    $item['coupon2'] = 1;
                }
                if ($item['coupon3']==0) {
                    $item['coupon3'] = 1;
                }
            }elseif($teamUserCount>=10){
                if ($item['coupon1']==0) {
                    $item['coupon1'] = 1;
                }
                if ($item['coupon2']==0) {
                    $item['coupon2'] = 1;
                }
                if ($item['coupon3']==0) {
                    $item['coupon3'] = 1;
                }
                if ($item['coupon4']==0) {
                    $item['coupon4'] = 1;
                }
            }
            return self::success(["team"=>$item]);
        }catch (\Exception $e){
            Log::error($e);
            return self::fail(500,"系统异常");
        }
    }


}